﻿// JavaScript File
/*
Google map Processing
Yang Xiaodong
2008-7-30
*/


function swift2GMap()
{
    if(MAP_TYPE != "GMAP")
    {
        gmap.clearOverlays();
        MAP_TYPE = "GMAP";
    }
}

function swift2IMS()
{
    if(MAP_TYPE != "IMSMAPONLY")
    {
        if(typeof(imsOverlay) != "undefined")
        {
            gmap.clearOverlays();

            //地图地理范围设置
            var mapSize = gmap.getBounds();
            
            // 设置叠加层的大小，这里设置为地图DIV的4倍大小，也就是4个方向均延升1倍
            //var bounds = map.getBounds();
            var southWest = mapSize.getSouthWest();
            var northEast = mapSize.getNorthEast();
            var lngDelta = (northEast.lng() - southWest.lng());// / 2;
            var latDelta = (northEast.lat() - southWest.lat());// / 2;
            var rectBounds = new GLatLngBounds(
                new GLatLng(southWest.lat() - latDelta,
                            southWest.lng() - lngDelta),
                new GLatLng(northEast.lat() + latDelta,
                            northEast.lng() + lngDelta));


            
            gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
            if(typeof(imsOverlay) != "undefined")
            {
                gmap.addOverlay(imsOverlay);
                $("#btnGMap").val("Google地图");
                MAP_TYPE = "IMSMAP";
            }
            MAP_TYPE = "IMSMAPONLY";
        }
        else
        {
            alert("您当前并没有绘制任何ArcIMS地图，所以不能切换到ArcIMS地图模式！");
            return;
        }
    }
}

function swift2Hybird()
{
    if(MAP_TYPE != "IMSMAP")
    {
        if(typeof(imsOverlay) != "undefined")
        {
            gmap.clearOverlays();
            gmap.addOverlay(imsOverlay);
            //$("#btnGMap").val("Google地图");
            MAP_TYPE = "IMSMAP";
        }  
        else
        {
            alert("您当前并没有绘制任何ArcIMS地图，所以不能切换到ArcIMS地图模式！");
            return;
        }
    }
}


function RefreshMap()
{
    if(MAP_TYPE == "IMSMAP" || MAP_TYPE == "IMSMAPONLY")
    {
        gmap.clearOverlays();
//******************************************************************88
        //getIMSMap(MAP_TYPE);
        var tempData=$("#forData").data("dt");
        if(tempData!=undefined)
        {
           if(tempData=="disease")
           {
              getIMSMap_Disease(MAP_TYPE);
              //alert("disease");
           }
           else
           {
              //alert("false");
              getIMSMap(MAP_TYPE);
           }
           
        }
        else
        {getIMSMap(MAP_TYPE);}
//******************************************************************88
    }
    else
    {
        alert("本按钮仅供刷新ArcIMS地图，请先生成相应的长势分级图！");
        return;
    }

}

/*
function CloseGMap()
{
    if(MAP_TYPE == "GMAP" || MAP_TYPE == "GIMAGE") 
    {
        alert("Google地图是当前唯一地图，不能关闭！");
        return;
    }
    gmap.clearOverlays();
    MAP_TYPE = "IMSMAPONLY";

    //地图地理范围设置
    var mapSize = gmap.getBounds();
    
    // 设置叠加层的大小，这里设置为地图DIV的2倍大小，也就是4个方向均延升1/2
    //var bounds = map.getBounds();
    var southWest = mapSize.getSouthWest();
    var northEast = mapSize.getNorthEast();
    var lngDelta = (northEast.lng() - southWest.lng()) / 2;
    var latDelta = (northEast.lat() - southWest.lat()) / 2;
    var rectBounds = new GLatLngBounds(
        new GLatLng(southWest.lat() - latDelta,
                    southWest.lng() - lngDelta),
        new GLatLng(northEast.lat() + latDelta,
                    northEast.lng() + lngDelta));


    
    gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
    gmap.addOverlay(imsOverlay);
    $("#btnCloseGmap").val("打开Google地图");
    //getIMSMap(MAP_TYPE);
}


function restoreGMap()
{
    if(MAP_TYPE != "GMAP" && MAP_TYPE != "GIMAGE")
    {
        gmap.clearOverlays();
        $("#btnGMap").val("IMS地图");
        MAP_TYPE = "GMAP";
    }
    else
    {
        if(typeof(imsOverlay) != "undefined")
        {
            gmap.addOverlay(imsOverlay);
            $("#btnGMap").val("Google地图");
            MAP_TYPE = "IMSMAP";
        }
    }
//        GLoadMap("");
    
}

*/

/*
Add Google Map at start.
*/
function GLoadMap(strLocation) {
    //resizePage();

    if (GBrowserIsCompatible()) 
    {
        var i = 0;
//        var addPoint = true;
//        var markerPoints = new Array();
//        
//        var contextmenu = document.createElement("div");
//        var menu1 = document.createElement("input");
//        menu1.type = "button";
//        menu1.value = "结束选点";
//        menu1.id = "end";
//        $(menu1).bind("click",function(){
//            contextmenu.style.visibility = "hidden"; 
//            window.open("SamplePoints.htm");
//            //将markerPoints作为参数传递到web服务，以调用RSIndex计算。
//        });
//        contextmenu.appendChild(menu1); 

//        $("#divTools").empty();       
        
        gmap = new GMap2(document.getElementById("map"));
        if (strLocation.length>0)
        {
            var lat = strLocation.split(",")[0];
            var lnt = strLocation.split(",")[1];
            gmap.setCenter(new GLatLng(parseFloat(lat),parseFloat(lnt)), 9);
        }
        else
        {
            gmap.setCenter(new GLatLng(39.913950, 116.405640), 9);
        }
        var largeMapControl=new GLargeMapControl();
        var mapTypeControl = new GMapTypeControl();
        
            
//        var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));
//        var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
        gmap.addControl(largeMapControl);//, topLeft);
        gmap.addControl(new GScaleControl());
//        gmap.addControl(mapTypeControl);
        
//        gmap.enableDoubleClickZoom();
        
//        GEvent.addListener(gmap, "click", function(overlay,latlng){
//            if(addPoint == true){
//                markerPoints[i] = latlng;
//                i++;
//                var marker = new GMarker(latlng);
//                gmap.addOverlay(marker);
//                rightClickResponse = true;
//            }
//        });
        
        GEvent.addListener(gmap,"zoomend",function(oldLevel, newLevel){
            //alert(MAP_TYPE);
            if(MAP_TYPE == "GMAP" || MAP_TYPE == "GIMAGE") return;
            
            gmap.clearOverlays();
            
            if(MAP_TYPE != "IMSMAPONLY")
                MAP_TYPE = "IMSMAP";
//*************************************************************8812-8日
            //getIMSMap(MAP_TYPE);
            var tempData=$("#forData").data("dt");
		    if(tempData!=undefined)
		    {
			    //alert($("#forData").data("dt"));
			    if(tempData=="disease")
			    {
			        getIMSMap_Disease(MAP_TYPE);
			    }else
			    {
			        getIMSMap(MAP_TYPE);
			    }
		    }else
		    {
			    //alert("no Data!");
			    getIMSMap(MAP_TYPE);
		    }
//**************************************************************8812-8日
        });

//        当拖动地图时，刷新IMS地图
//******************************************************
//        GEvent.addListener(gmap,"dragend",function(){
//            //alert(MAP_TYPE);
//            if(MAP_TYPE != "IMSMAP") return;
//            gmap.clearOverlays();
//            getIMSMap(MAP_TYPE);
//        });
        
        
//          鼠标右键单击事件，添加标记点
//****************************************************************************        
//        GEvent.addListener(gmap,"singlerightclick",function(pixel,tile, marker) {   
//            // store the "pixel" info in case we need it later   
//            // adjust the context menu location if near an egde   
//            // create a GControlPosition   
//            // apply it to the context menu, and make the context menu visible 
//            if(rightClickResponse == true){  
//                clickedPixel = pixel;   
//                var x=pixel.x;   
//                var y=pixel.y;   
//                if (x > gmap.getSize().width - 120) { x = gmap.getSize().width - 120 }   
//                if (y > gmap.getSize().height - 100) { y = gmap.getSize().height - 100 }   
//                var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));     
//                pos.apply(contextmenu);  
//                
//                contextmenu.style.visibility = "visible"; 
//                $("#map").append(contextmenu); 
//                addPoint = false;
//                rightClickResponse = false; 
//            }
//        });   
     MAP_TYPE = "GMAP";
     
//	document.onmousedown = handleMouseDown;
//	document.onmousemove = handleMouseMove;
//	document.onmouseup = handleMouseUp;
	
     
  }
}

function createContextMenu()
{
    var contextmenu = document.createElement("div");
    var menu1 = document.createElement("a");
    menu1.href = "#";
    var menu1Text = document.createTextNode("End");
    menu1.appendChild(menu1Text);
    contextmenu.appendChild(menu1);
    return contextmenu;
}


/*
**通过point信息对地图上面进行标注，显示相应点上的标注信息
*/
function createMarker(point, map, myHtml) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {

    marker.openInfoWindowHtml(myHtml);
   
  });
  map.addOverlay(marker);
 
}


/*
** 依据地点信息文件cities.xml，对地图进行标注
*/
function markCity(map,point){
   
	    var myHtml;
        myHtml = "<a href = '#'>" + "Hello" + "</a><br/>";
        createMarker(point, map, myHtml);        

}


//处理IMS地图服务的请求
function getIMSMap(event)
{
    var imageId = $("#ImageListSelect").val();
    
    var graduations = "";
    var labels = "";
    var colors = "";
    var graduationNumber = $("#GraduationNumber").val();
    if (graduationNumber > 0)
    {
        if($("#defGraduations").attr("checked") == true)  
        {  
            graduations = "system," + graduationNumber;
        }
        else
        {
        
            var lastValue = 0;
            for (var i = 0; i < graduationNumber; i++)
            {
                var graValue = eval($("#Graduation" + i)).val();

                if (graValue.lenght == 0) {alert("请填写完整的刻度分级数值！");return;}
                if(!isDigit(graValue)) {alert("您填写的分级刻度包含非数值字符，请重新填写！");return;}
                if(graValue < lastValue) {alert("您填写的分级刻度值小于零或者小于上一级刻度值，请重新检查！");return;}
                graduations += graValue + ",";
                lastValue = parseFloat(graValue);
                var graLabel = eval($("#Label" + i)).val();
                labels += graLabel + ",";
                
                var graColor = eval($("#Color" + i)).val();
                colors += graColor + ",";
            }
        }
        if($("#defLabels").attr("checked") == true) 
        {
            labels = "system";
        }
        else
        {
            for (var i = 0; i < graduationNumber; i++)
            {
                var graLabel = eval($("#Label" + i)).val();
                labels += graLabel + ",";
            }
        }
        if($("#defColors").attr("checked") == true) 
        {
            colors = "system";
        }
        else
        {
            for (var i = 0; i < graduationNumber; i++)
            {
                var graColor = eval($("#Color" + i)).val();
                colors += graColor + ",";
            }
        }
    }
    var layerName="";//11-18日添加，用于AXL中的LAYER的name属性
    layerName="长势分级";
    
    MAP_TYPE = "IMSMAP";//默认情况下设置MAP_TYPE为请求IMS地图服务
    if (event.data == null)
    {
        MAP_TYPE = event;
    }
    else
    {
        MAP_TYPE = event.data.flag;
        layerName=event.data.layername;//11-18日添加，用于AXL中的LAYER的name属性
    }
    
    //图片大小设置
    var divWidth = $("#map").width() * 2; //gmap.getSize().width - 6;    
    var divHeight = $("#map").height() * 2;//gmap.getSize().height - 6;  
    if(MAP_TYPE == "IMSLEGEND") divWidth -= 258;
    
    //地图地理范围设置
    var mapSize = gmap.getBounds();
    
    // 设置叠加层的大小，这里设置为地图DIV的2倍大小，也就是4个方向均延升1/2
    //var bounds = map.getBounds();
    var southWest = mapSize.getSouthWest();
    var northEast = mapSize.getNorthEast();
    var lngDelta = (northEast.lng() - southWest.lng()) / 2;
    var latDelta = (northEast.lat() - southWest.lat()) / 2;
    var rectBounds = new GLatLngBounds(
        new GLatLng(southWest.lat() - latDelta,
                    southWest.lng() - lngDelta),
        new GLatLng(northEast.lat() + latDelta,
                    northEast.lng() + lngDelta));
    
    
    
    var mapEast = parseInt(10000*rectBounds.getNorthEast().lng())/10000;
    var mapNorth = parseInt(10000*rectBounds.getNorthEast().lat())/10000;
    var mapWest = parseInt(10000*rectBounds.getSouthWest().lng())/10000;
    var mapSouth = parseInt(10000*rectBounds.getSouthWest().lat())/10000;
    var mapBox = mapWest + "," + mapEast +  "," + mapNorth +  "," + mapSouth;
    
    //选择的模型方程及参数
    var selectedModelName = "";
    var selectedParamA = "";
    var selectedParamB = "";
    var selectedModel = "";
    var tempString = "";
    var tempStringArray = new Array();
    
    if($("#radioLine").attr("checked") == true) 
    {
        selectedModelName = "LinerModel";
        selectedModel = $("#txtLine").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
//        selectedParamB = tempString.substring(1);
//        selectedParamA = $("#txtLine").val();
//        selectedParamB = $("#txtLineR").val();
    }
    if($("#radioExp").attr("checked") == true) 
    {
        selectedModelName = "ExpModel";
        selectedModel = $("#txtExp").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
    }
    if($("#radioLn").attr("checked") == true) 
    {
        selectedModelName = "LogModel";
        selectedModel = $("#txtLn").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
    }
    
        
    
    //请求地图
    showWaitImage();

    var requestBaseUrl = "services/GetMap.aspx";
    $.get(requestBaseUrl,{  SERVICENAME:"TEMP1",
                            MAP_TYPE:MAP_TYPE,
                            MAPBOX:mapBox,
                            MAPWIDTH:divWidth,
                            MAPHEIGHT:divHeight,
                            GRADUATIONS:graduations,
                            LABELS:labels,
                            COLORS:colors,
                            MODELNAME:selectedModelName,
                            PARAMA:selectedParamA,
                            PARAMB:selectedParamB,
                            IMAGEID:imageId,
                            LAYERNAME:layerName,
                            TMP:Math.random()                           
                            },
                            function(data){
                                //alert(data.documentElement);
                                var xmlDocument = data.documentElement;
                                var nodes = xmlDocument.getElementsByTagName("URL");
                                //*********************************************************
                                //12-4日
//                                var my_mapBox_nodes=xmlDocument.getElementsByTagName("ENVELOPE");
//                                var my_mapBox=my_mapBox_nodes[0].firstChild.nodeValue;
//                                var array_mapBox=my_mapBox.split(',');
                                //*********************************************************
                                var strResultUrl = "";
                                if(nodes[0].firstChild != null)
                                    strResultUrl = nodes[0].firstChild.nodeValue;
                                else
                                    strResultUrl = "Picture/mapnotexist.gif";
                                if(strResultUrl.length == 0)
                                    strResultUrl = "Picture/mapnotexist.gif";
//                                hideWaitImage();
//                                return strResultUrl;
                                if(MAP_TYPE == "IMSMAP" || MAP_TYPE == "IMSMAPONLY")
                                {
//                                    var imgMap = document.createElement("img");
//                                    imgMap.src = strResultUrl;
//                                    imgMap.alt = "长势分级图";
                                    gmap.clearOverlays();
//                                    gmap.addOverlay(new ImsMapOverlay(rectBounds));

                                    if (MAP_TYPE == "IMSMAPONLY")
                                        gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
                                    imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
                                    gmap.addOverlay(imsOverlay);
                                                            
//                                    if (MAP_TYPE == "IMSMAP")
//                                        gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
//                                    var rectBounds = new GLatLngBounds(
//                                            new GLatLng(southWest.lat() - latDelta,
//                                                        southWest.lng() - lngDelta),
//                                            new GLatLng(northEast.lat() + latDelta,
//                                                        northEast.lng() + lngDelta));
//***************************************************************************************************************************************//
//12-4日增加判断
//                                    if(array_mapBox.length>0)
//                                    {
//                                        var new_rectBounds=new GLatLngBounds(new GLatLng(array_mapBox[1]- latDelta,array_mapBox[0]- lngDelta),
//                                                                             new GLatLng(array_mapBox[3]+ latDelta,array_mapBox[2]+ lngDelta));
//                                        imsOverlay = new ImsMapOverlay(new_rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
//                                    }
//                                    else
//                                    {
//                                        imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
//                                    }
//***************************************************************************************************************************************//
                                    //imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
                                    //gmap.addOverlay(imsOverlay);
//                                    gmap.addOverlay(new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc"));
                                    
                                    
//                                    if(MAP_TYPE == "IMSLEGEND"){
//                                        strResultUrl = xmlDocument.childNodes[1].nodeValue;
//                                        var imgLegend = document.createElement("img");
//                                        imgLegend.src = strResultUrl;
//                                        imgLegend.alt = "图例";
//                                        $('#legendDiv').empty();
//                                        $('#legendDiv').append(imgLegend);
//                                    }
                                    
//                                    $("#map").empty();
//                                    $("#map").append(imgMap);
//                                    $("#map").css("cursor","hand");
//                                    $("#map").bind("mousedown",handleMouseDown());
//                                    $("#map").bind("mouseup",handleMouseUp());
//                                    if(MAP_TYPE != "IMSMAP") 
//                                    {
//                                        MAP_TYPE = "IMSMAP";
//                                    }
                                    hideWaitImage(); 
                                }
                                else if(MAP_TYPE == "IMSLEGEND")
                                {
                                    var imgLegend = document.createElement("img");
                                    imgLegend.src = strResultUrl;
                                    imgLegend.alt = "图例";
                                    $('#legendDiv').empty();
                                    $('#legendDiv').append(imgLegend);
                                    
                                    MAP_TYPE = lastMapType;//恢复生成图例之前的地图类型；
                                    hideWaitImage(); 
                                }
//                                createIMSToos();                               
//                                hideWaitImage(); 
                                
                            });
}

//function getIMSLegend(flag)
//{
//    var strResultUrl = createIMSMap(event.data.flag);
//    var imgMap = document.createElement("img");
//    imgMap.src = strResultUrl;
//    imgMap.alt = "长势分级图";
//    $("#map").empty();
//    $("#map").append(imgMap);
//}

//function createIMSMap(flag)
//{

//    var graduations = "";
//    var graduationNumber = $("#GraduationNumber").val();
//    if (graduationNumber > 0)
//    {
//        for (var i = 0; i < graduationNumber; i++)
//        {
//            var graValue = eval($("#Graduation" + i)).val();
//            if (graValue.lenght == 0) {alert("请填写完整的刻度分级数值！");return;}
//            graduations += graValue + ",";
//        }
//    }
//    
//    
//    var divWidth = gmap.getSize().width - 6;    
//    var divHeight = gmap.getSize().height - 6;  
//    if(flag == "IMSLEGEND") divWidth -= 264;
//    
//    var mapSize = gmap.getBounds();
//    
//    var mapEast = parseInt(10000*mapSize.getNorthEast().lng())/10000;
//    var mapNorth = parseInt(10000*mapSize.getNorthEast().lat())/10000;
//    var mapWest = parseInt(10000*mapSize.getSouthWest().lng())/10000;
//    var mapSouth = parseInt(10000*mapSize.getSouthWest().lat())/10000;
//    var mapBox = mapWest + "," + mapEast +  "," + mapNorth +  "," + mapSouth;
    
//    var requestBaseUrl = 'services/GetMap.aspx?servicename=BJ1&flag=' + flag;
//    var requestUrl = requestBaseUrl   + "&mapBox=" +  mapBox
//                                      + "&mapWidth=" + divWidth
//                                      + "&mapheight=" + divHeight
//                                      + "&graduations=" + graduations;


//    showWaitImage();
//    var requestBaseUrl = "services/getThematicMap.aspx";
//    var requestUrl = requestBaseUrl   + "graduations=" + graduations;
//    $.get(requestBaseUrl,{  SERVICENAME:"BJ1",
//                            FLAG:flag,
//                            MAPBOX:mapBox,
//                            MAPWIDTH:divWidth,
//                            MAPHEIGHT:divHeight,
//                            GRADUATIONS:graduations                           
//                            },
//                            function(data){
//                                alert(data.documentElement);
//                                return data;
//                            });

    
                                                
//     var request = createRequest(); 
//     var myXD = new ActiveXObject("MSXML2.DOMDocument");

//     try
//     {
//         request.open("GET",requestUrl,false);
//         request.send();
//         //myXD.loadXML(request.responseText);
//         var strResultUrl = request.responseText;
//         return strResultUrl;
//         
//     }catch(err)
//     {
//        
//     }
    
    
//}





/*
Call cached ArcIMS service for GoogleMaps v2
Jeremy Bartley, Kansas Geological Survey
Inspiration & Code from:
        John Deck for reworking the overlay code to work with Gooogle Maps version2
	Mike Williams http://www.econym.demon.co.uk/googlemaps2/ V2 Reference & custommap code
	Brian Flood http://www.spatialdatalogic.com/cs/blogs/brian_flood/archive/2005/07/11/39.aspx V1 WMS code
	Kyle Mulka http://blog.kylemulka.com/?p=287  V1 WMS code modifications
	http://search.cpan.org/src/RRWO/GPS-Lowrance-0.31/lib/Geo/Coordinates/MercatorMeters.pm
*/

var MAGIC_NUMBER=6356752.3142;
var DEG2RAD=0.0174532922519943;
var PI=3.14159267;
function dd2MercMetersLng(p_lng) { 
	return MAGIC_NUMBER*(p_lng*DEG2RAD); 
}

function dd2MercMetersLat(p_lat) {
	if (p_lat >= 85) p_lat=85;
	if (p_lat <= -85) p_lat=-85;
	return MAGIC_NUMBER*Math.log(Math.tan(((p_lat*DEG2RAD)+(PI/2)) /2));
}

CustomGetTileUrl=function(a,b,c) {
	if (typeof(window['this.myStyles'])=="undefined") this.myStyles=""; 
	var ts = 256;
	var lULP = new GPoint(a.x*256,(a.y+1)*256);
	var lLRP = new GPoint((a.x+1)*256,a.y*256);
	var UL = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lULP,b,c);
	var LR = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lLRP,b,c);
        var bbox = UL.x + "," + UL.y + "," + LR.x + "," + LR.y;
        var newzoom = gmap.getZoom();
        var oldzoom = 17 - newzoom;
        var url = this.myBaseURL + "&BBOX=" + bbox 
                                 + "&WIDTH=" + ts 
                                 + "&HEIGHT=" + ts 
                                 + "&X=" + a.x 
                                 + "&Y=" + a.y 
                                 + "&zoom=" + oldzoom
                                 + "&newzoom=" + newzoom;
        
//         var request = createRequest(); 
//         var myXD = new ActiveXObject("MSXML2.DOMDocument");

//         try
//         {
//             request.open("GET",url,false);
//             request.send();
//             //myXD.loadXML(request.responseText);
//             var strResultUrl = request.responseText;
//             
//             return strResultUrl;
//         }
//         catch(error)
//         {
            if(a.x % 4 == 0)                         
                return 'http://192.168.2.51/rscrop/images/BJ1_rserver67321840324.png';//url;
            else if(a.x % 4 == 1)
                return 'http://192.168.2.51/rscrop/images/BJ1_rserver67321840326.png';
            else if(a.x % 4 == 2)
                return 'http://192.168.2.51/rscrop/images/BJ1_rserver67321840328.png';
            else
                return 'http://192.168.2.51/rscrop/images/BJ1_rserver67321840330.png';
//         }
     
             
	// switch between Mercator and DD if merczoomlevel is set

//	if (this.myMercZoomLevel!=0 && map.getZoom() < this.myMercZoomLevel) {
//    	var lBbox=dd2MercMetersLng(lUL.x)+","+dd2MercMetersLat(lUL.y)+","+dd2MercMetersLng(lLR.x)+","+dd2MercMetersLat(lLR.y);
//    	var lSRS="EPSG:54004";
//	} else {
//    	var lBbox=lUL.x+","+lUL.y+","+lLR.x+","+lLR.y;
//    	var lSRS="EPSG:4326";
//	}
//	var lURL=this.myBaseURL;
//	lURL+="&REQUEST=GetMap";
//	lURL+="&SERVICE=WMS";
//	lURL+="&VERSION=1.1.1";
//	lURL+="&LAYERS="+this.myLayers;
//	lURL+="&STYLES="+this.myStyles; lURL+="&FORMAT="+this.myFormat;
//	lURL+="&BGCOLOR=0xFFFFFF";
//	lURL+="&TRANSPARENT=TRUE";
//	lURL+="&SRS="+lSRS;
//	lURL+="&BBOX="+lBbox;
//	lURL+="&WIDTH=256";
//	lURL+="&HEIGHT=256";
//	lURL+="&reaspect=false";
//	return lURL;
}

function getCropMAP(event)
{
    var imageId = $("#ImageListSelect").val();//得到要查询的图像的年
    
    var graduations = "";
    var labels = "";
    var colors = "";
    var graduationNumber = $("#levelhidde").val();
    if (graduationNumber > 0)
    {
        var lastValue = 0;
        for (var i = 0; i < graduationNumber; i++)
        {
            var graValue = eval($("#Graduation" + i)).val();

            if (graValue.lenght == 0) {alert("请填写完整的刻度分级数值！");return;}
            if(!isDigit(graValue)) {alert("您填写的分级刻度包含非数值字符，请重新填写！");return;}
            if(graValue < lastValue) {alert("您填写的分级刻度值小于零或者小于上一级刻度值，请重新检查！");return;}
            graduations += graValue + ",";
            lastValue = parseFloat(graValue);
            var graLabel = eval($("#Label" + i)).val();
            labels += graLabel + ",";
            
            var graColor = eval($("#Color" + i)).val();
            colors += graColor + ",";
        }
    }
    
     MAP_TYPE = "IMSMAP";//默认情况下设置MAP_TYPE为请求IMS地图服务
    if (event.data == null)
    {
        MAP_TYPE = event;
    }
    else
    {
        MAP_TYPE = event.data.flag;
    }
    
    //图片大小设置
    var divWidth = $("#map").width() * 2; //gmap.getSize().width - 6;    
    var divHeight = $("#map").height() * 2;//gmap.getSize().height - 6;  
    if(MAP_TYPE == "IMSLEGEND") divWidth -= 258;
    
    //地图地理范围设置
    var mapSize = gmap.getBounds();
    
    // 设置叠加层的大小，这里设置为地图DIV的2倍大小，也就是4个方向均延升1/2
    //var bounds = map.getBounds();
    var southWest = mapSize.getSouthWest();
    var northEast = mapSize.getNorthEast();
    var lngDelta = (northEast.lng() - southWest.lng()) / 2;
    var latDelta = (northEast.lat() - southWest.lat()) / 2;
    var rectBounds = new GLatLngBounds(
        new GLatLng(southWest.lat() - latDelta,
                    southWest.lng() - lngDelta),
        new GLatLng(northEast.lat() + latDelta,
                    northEast.lng() + lngDelta));
    
    
    
    var mapEast = parseInt(10000*rectBounds.getNorthEast().lng())/10000;
    var mapNorth = parseInt(10000*rectBounds.getNorthEast().lat())/10000;
    var mapWest = parseInt(10000*rectBounds.getSouthWest().lng())/10000;
    var mapSouth = parseInt(10000*rectBounds.getSouthWest().lat())/10000;
    var mapBox = mapWest + "," + mapEast +  "," + mapNorth +  "," + mapSouth;
    
    //选择的模型方程及参数
    var selectedModelName = "";
    var selectedParamA = "";
    var selectedParamB = "";
    var selectedModel = "";
    var tempString = "";
    var tempStringArray = new Array();
    
    if($("#radioLine").attr("checked") == true) 
    {
        selectedModelName = "LinerModel";
        selectedModel = $("#txtLine").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
    }
    if($("#radioExp").attr("checked") == true) 
    {
        selectedModelName = "ExpModel";
        selectedModel = $("#txtExp").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
    }
    if($("#radioLn").attr("checked") == true) 
    {
        selectedModelName = "LogModel";
        selectedModel = $("#txtLn").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
    }
    
    showWaitImage();
    
    var requestBaseUrl = "services/getCropMap.aspx";
    $.get(requestBaseUrl,{  SERVICENAME:"TEMP1",
                            MAP_TYPE:MAP_TYPE,
                            MAPBOX:mapBox,
                            MAPWIDTH:divWidth,
                            MAPHEIGHT:divHeight,
                            GRADUATIONS:graduations,
                            LABELS:labels,
                            COLORS:colors,
                            MODELNAME:selectedModelName,
                            PARAMA:selectedParamA,
                            PARAMB:selectedParamB,
                            IMAGEID:imageId,
                            TMP:Math.random()                           
                            },
                            function(data){
                                var xmlDocument = data.documentElement;
                                var nodes = xmlDocument.getElementsByTagName("URL");
                                var strResultUrl = "";
                                if(nodes[0].firstChild != null)
                                    strResultUrl = nodes[0].firstChild.nodeValue;
                                else
                                    strResultUrl = "Picture/mapnotexist.gif";
                                if(strResultUrl.length == 0)
                                    strResultUrl = "Picture/mapnotexist.gif";

                                if(MAP_TYPE == "IMSMAP" || MAP_TYPE == "IMSMAPONLY")
                                {

                                    gmap.clearOverlays();                      
                                                            
//                                    if (MAP_TYPE == "IMSMAPONLY")
//                                        gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
                                    if(colors!="")
                                    {
                                        imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",1,colors);
                                        gmap.addOverlay(imsOverlay);
                                    }else
                                    {
                                        imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
                                        gmap.addOverlay(imsOverlay);
                                    }
                                    

                                    hideWaitImage(); 
                                }
                                else if(MAP_TYPE == "IMSLEGEND")
                                {
                                    var imgLegend = document.createElement("img");
                                    imgLegend.src = strResultUrl;
                                    imgLegend.alt = "图例";
                                    $('#legendDiv').empty();
                                    $('#legendDiv').append(imgLegend);
                                    
                                    MAP_TYPE = lastMapType;//恢复生成图例之前的地图类型；
                                    hideWaitImage(); 
                                }
                            });
}
//*****************************************************灾害监测-病害监测（宫）*********************************************************************************
function getIMSMap_Disease(event)
{
    var imageId = $("#ImageListSelect").val();
    
    var graduations = "";
    var labels = "";
    var colors = "";
    var graduationNumber = $("#GraduationNumber").val();
    if (graduationNumber > 0)
    {
        if($("#defGraduations").attr("checked") == true)  
        {  
            graduations = "system," + graduationNumber;
        }
        else
        {
        
            var lastValue = 0;
            for (var i = 0; i < graduationNumber; i++)
            {
                var graValue = eval($("#Graduation" + i)).val();

                if (graValue.lenght == 0) {alert("请填写完整的刻度分级数值！");return;}
                if(!isDigit(graValue)) {alert("您填写的分级刻度包含非数值字符，请重新填写！");return;}
                if(graValue < lastValue) {alert("您填写的分级刻度值小于零或者小于上一级刻度值，请重新检查！");return;}
                graduations += graValue + ",";
                lastValue = parseFloat(graValue);
    //            if(i > 0) 
    //            {
                var graLabel = eval($("#Label" + i)).val();
                labels += graLabel + ",";
                
                var graColor = eval($("#Color" + i)).val();
                colors += graColor + ",";
    //            }
            }
        }
        if($("#defLabels").attr("checked") == true) labels = "system";
        if($("#defColors").attr("checked") == true) colors = "system";
        
    }
    
    
    MAP_TYPE = "IMSMAP";//默认情况下设置MAP_TYPE为请求IMS地图服务
    if (event.data == null)
    {
        MAP_TYPE = event;
    }
    else
    {
        MAP_TYPE = event.data.flag;
    }
    
    //图片大小设置
    var divWidth = $("#map").width() * 2; //gmap.getSize().width - 6;    
    var divHeight = $("#map").height() * 2;//gmap.getSize().height - 6;  
    if(MAP_TYPE == "IMSLEGEND") divWidth -= 258;
    
    //地图地理范围设置
    var mapSize = gmap.getBounds();
    
    // 设置叠加层的大小，这里设置为地图DIV的2倍大小，也就是4个方向均延升1/2
    //var bounds = map.getBounds();
    var southWest = mapSize.getSouthWest();
    var northEast = mapSize.getNorthEast();
    var lngDelta = (northEast.lng() - southWest.lng()) / 2;
    var latDelta = (northEast.lat() - southWest.lat()) / 2;
    var rectBounds = new GLatLngBounds(
        new GLatLng(southWest.lat() - latDelta,
                    southWest.lng() - lngDelta),
        new GLatLng(northEast.lat() + latDelta,
                    northEast.lng() + lngDelta));
    
    
    
    var mapEast = parseInt(10000*rectBounds.getNorthEast().lng())/10000;
    var mapNorth = parseInt(10000*rectBounds.getNorthEast().lat())/10000;
    var mapWest = parseInt(10000*rectBounds.getSouthWest().lng())/10000;
    var mapSouth = parseInt(10000*rectBounds.getSouthWest().lat())/10000;
    var mapBox = mapWest + "," + mapEast +  "," + mapNorth +  "," + mapSouth;
    
    //选择的模型方程及参数
    var selectedModelName = "";
    var selectedParamA = "";
    var selectedParamB = "";
    var selectedModel = "";
    var tempString = "";
    var tempStringArray = new Array();
    
    if($("#radioLine").attr("checked") == true) 
    {
        selectedModelName = "LinerModel";
        selectedModel = $("#txtLine").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
//        selectedParamB = tempString.substring(1);
//        selectedParamA = $("#txtLine").val();
//        selectedParamB = $("#txtLineR").val();
    }
    if($("#radioExp").attr("checked") == true) 
    {
        selectedModelName = "ExpModel";
        selectedModel = $("#txtExp").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
    }
    if($("#radioLn").attr("checked") == true) 
    {
        selectedModelName = "LogModel";
        selectedModel = $("#txtLn").val();
        tempString = selectedModel.split('=')[1];
        tempStringArray = tempString.split('+');
        selectedParamA = tempStringArray[0];
        tempString = tempStringArray[1].split(')')[0];
        selectedParamB = tempString.split('(')[1];
    }
    
        
    
    //请求地图
    showWaitImage();
    var requestBaseUrl = "services/GetMap_Disease.aspx";
    $.get(requestBaseUrl,{  SERVICENAME:"TEMP1",
                            MAP_TYPE:MAP_TYPE,
                            MAPBOX:mapBox,
                            MAPWIDTH:divWidth,
                            MAPHEIGHT:divHeight,
                            GRADUATIONS:graduations,
                            LABELS:labels,
                            COLORS:colors,
                            MODELNAME:selectedModelName,
                            PARAMA:selectedParamA,
                            PARAMB:selectedParamB,
                            IMAGEID:imageId,
                            TMP:Math.random()                           
                            },
                            function(data){
                                //alert(data.documentElement);
                                var xmlDocument = data.documentElement;
                                var nodes = xmlDocument.getElementsByTagName("URL");
                                var strResultUrl = "";
                                if(nodes[0].firstChild != null)
                                    strResultUrl = nodes[0].firstChild.nodeValue;
                                else
                                    strResultUrl = "Picture/mapnotexist.gif";
                                if(strResultUrl.length == 0)
                                    strResultUrl = "Picture/mapnotexist.gif";
//                                hideWaitImage();
//                                return strResultUrl;
                                if(MAP_TYPE == "IMSMAP" || MAP_TYPE == "IMSMAPONLY")
                                {
//                                    var imgMap = document.createElement("img");
//                                    imgMap.src = strResultUrl;
//                                    imgMap.alt = "病虫害分级图";
                                    gmap.clearOverlays();
//                                    gmap.addOverlay(new ImsMapOverlay(rectBounds));

                      
                                                            
                                    if (MAP_TYPE == "IMSMAPONLY")
                                        gmap.addOverlay(new ImsMapOverlay(rectBounds,"","#dcdcdc",0,"#dcdcdc"));
                                    imsOverlay = new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc");
                                    gmap.addOverlay(imsOverlay);
//                                    gmap.addOverlay(new ImsMapOverlay(rectBounds,strResultUrl,"transparent",0,"#dcdcdc"));
                                    
                                    
//                                    if(MAP_TYPE == "IMSLEGEND"){
//                                        strResultUrl = xmlDocument.childNodes[1].nodeValue;
//                                        var imgLegend = document.createElement("img");
//                                        imgLegend.src = strResultUrl;
//                                        imgLegend.alt = "图例";
//                                        $('#legendDiv').empty();
//                                        $('#legendDiv').append(imgLegend);
//                                    }
                                    
//                                    $("#map").empty();
//                                    $("#map").append(imgMap);
//                                    $("#map").css("cursor","hand");
//                                    $("#map").bind("mousedown",handleMouseDown());
//                                    $("#map").bind("mouseup",handleMouseUp());
//                                    if(MAP_TYPE != "IMSMAP") 
//                                    {
//                                        MAP_TYPE = "IMSMAP";
//                                    }
                                    hideWaitImage(); 
                                }
                                else if(MAP_TYPE == "IMSLEGEND")
                                {
                                    var imgLegend = document.createElement("img");
                                    imgLegend.src = strResultUrl;
                                    imgLegend.alt = "图例";
                                    $('#legendDiv').empty();
                                    $('#legendDiv').append(imgLegend);
                                    
                                    MAP_TYPE = lastMapType;//恢复生成图例之前的地图类型；
                                    hideWaitImage(); 
                                }
//                                createIMSToos();                               
//                                hideWaitImage(); 
                                
                            });
}
//**************************************************************************************************************************************